package com.bandroid.demo.view;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bandroid.demo.R;
import com.bandroid.demo.base.MyActivity;
import com.base.balibrary.base.BaseActivity;
import com.base.balibrary.ioc.BaseContentView;
import com.base.balibrary.ioc.BaseOnClick;
import com.base.balibrary.ioc.BaseViewInject;
import com.base.balibrary.utils.LoadImage.DisplayImg;
import com.base.balibrary.utils.Web.BaseWeb;

import java.io.File;

/**
 * BAndroid
 * Created by YichenZ on 2015/8/5 10:50.
 */
@BaseContentView(R.layout.activity_web)
public class WebActivity extends MyActivity {
    @BaseViewInject(R.id.dataWv)
    WebView dataWv;
    @BaseViewInject(R.id.backIBtn)
    ImageButton backIBtn;
    @BaseViewInject(R.id.titleRl)
    RelativeLayout titleRl;
    @BaseViewInject(R.id.titleTv)
    TextView titleTv;
    @BaseViewInject(R.id.welcomeIv)
    ImageView welcomeIv;
    final String[] BASE_URL={"http://test.awu.cn/data/upload/home/awu","http://yh.120yibao.com/p/index"};
    final String[] BASE_URL_TOP={"http://yh.120yibao.com/p/order","http://yh.120yibao.com/p/my"};
    private static final String TAG = WebActivity.class.getSimpleName();

    //缓存当前URL
    String cacheUrl="";

    WebSettings settings;
    BaseWeb baseWeb;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initUI();
        initData();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(baseWeb!=null) {
            settings=null;
            baseWeb = null;
        }
    }

    private void initUI() {
        DisplayImg.getInstance().displayImg(this, welcomeIv, "http://img.120yibao.com/ad7.jpg");
    }

    private void initData() {
        baseWeb=new BaseWeb();
        WebViewClient webViewClient = new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if(url.substring(0,3).equals("tel")){
                    Intent it = new Intent(Intent.ACTION_DIAL, Uri.parse(url.substring(0,4)
                            + url.substring(4)));
                    it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(it);
                }else{
                    view.loadUrl(url);
                }
                return true;
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                mApplication.logMessage(url);
                cacheUrl=url;
                if(url.equals(BASE_URL[0]) || url.equals(BASE_URL[1])){
                    titleRl.setVisibility(View.GONE);
                }else{
                    titleRl.setVisibility(View.VISIBLE);
                }

            }

            @Override
            public void onPageFinished(WebView view, String url) {
                if(!settings.getLoadsImagesAutomatically()) {
                    settings.setLoadsImagesAutomatically(true);
                }
            }
        };
        WebChromeClient webChromeClient =new WebChromeClient(){
            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
                titleTv.setText(title);
            }

            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                if(welcomeIv.getVisibility()==View.VISIBLE){
                    if(newProgress>=100){

                        welcomeIv.setVisibility(View.GONE);
                    }
                }
            }
        };
        dataWv.setWebViewClient(webViewClient);
        dataWv.setWebChromeClient(webChromeClient);
        baseWeb.setSettings(dataWv.getSettings());
        settings=baseWeb.setWeb(this);
        dataWv.loadUrl(BASE_URL[0]);

    }

    @BaseOnClick({R.id.backIBtn})
    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()){
            case R.id.backIBtn: {
                if(dataWv.canGoBack()){
                    isGoBack();
                }else{
                    onDoubleClickBackExit();
                }
                break;
            }
        }
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && dataWv.canGoBack()) {
            return isGoBack();
        }
        return super.onKeyDown(keyCode, event);
    }

    private boolean isGoBack(){
        for(String url:BASE_URL_TOP){
            if(cacheUrl.equals(url)){
                dataWv.loadUrl(BASE_URL[0]);
                dataWv.clearHistory();
                return true;
            }
        }
        for(String url:BASE_URL){
            if(cacheUrl.equals(url)){
                onDoubleClickBackExit();
                return true;
            }
        }
        dataWv.goBack();
        return true;
    }
    protected long mExitTime;
    protected boolean onDoubleClickBackExit(){
        if ((System.currentTimeMillis() - mExitTime) > 2000) {
            toastMessage(this, "再按一次退出程序", Toast.LENGTH_SHORT);
            mExitTime = System.currentTimeMillis();
        } else {
            baseWeb.clearWebViewCache(this);
            finishActivity();
        }
        return true;
    }



}
